package com.ys.controller.web.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.finance.TcwWanliOtherPaymentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwWanliOtherPayment;
import com.ys.entity.finance.TcwWanliOtherPaymentItem;
import com.ys.enume.finance.TcwWanliOtherPaymentEnum;
import com.ys.enume.flow.FlowEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/finance/tcwWanliOtherPayment")
public class WebTcwWanliOtherPaymentController {
	
	@Autowired
	private CommonApiService<TcwWanliOtherPayment> commonApiService;

	@Autowired
	private TcwWanliOtherPaymentService tcwWanliOtherPaymentService;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,
			Integer payType){
		
		if (payType == null) {
			return W.error(model, "支付类型不能为空");
		}

		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TCW_WanliOtherPayment);
			
			TcwWanliOtherPayment entity = new TcwWanliOtherPayment()
					.setBid(bid)
					.setPayType(payType)
					.setCreateTime(new Date());
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliOtherPayment, new ArrayList<Map<String, Object>>());
			
			String aa = commonApiService.toAdd(model, entity, MenuId.TCW_WanliOtherPayment, null, subGridConfig);
            
            if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
    			return "finance/tcwWanliOtherPayment/toAdd";
			} else {
				return aa;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 编辑
	 */
	@RequiresPermissions("tcwWanliOtherPayment:add")
	@GetMapping("/toFlow")
	public String toFlow(Model model, HttpServletRequest request, HttpServletResponse response,
			String bid){
		if (bid == null || bid.length() == 0) {
			return W.error(model, "单号不能为空");
		}

		try {
			Map<String, Object> extraParam = tcwWanliOtherPaymentService.getExtraParam(bid);
			// 子列表配置信息
			List<Map<String, Object>> sonlist = tcwWanliOtherPaymentService.getItemList(bid);
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliOtherPayment, sonlist);
			
			String aa = commonApiService.toEdit(model, Long.parseLong(extraParam.get("id").toString()), new TcwWanliOtherPayment(), MenuId.TCW_WanliOtherPayment, extraParam, subGridConfig);
            
            if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
    			return "finance/tcwWanliOtherPayment/toEdit";
			} else {
				return aa;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}
	
	/*
	 * 编辑
	 */
	@RequiresPermissions("tcwWanliOtherPayment:batchEditExchangeRate")
	@GetMapping("/toBatchEditExchangeRate")
	public String toBatchEditExchangeRate(Model model, HttpServletRequest request, HttpServletResponse response,
			String bids){
		
//		List<String> listBid = new ArrayList<String>();
//		List<String> listBidUnUse = new ArrayList<String>();
//		for(String bid : bids.split(",")) {
//			if (listBid.contains(bid)) {
//				continue;
//			}
//			if (listBidUnUse.contains(bid)) {
//				continue;
//			}
//			
//			if(new TcwWanliOtherPayment().setBid(bid).setEffectStock(FlowEnum.EffectStock.EFFECTED.value).count() == 0) {
//				listBidUnUse.add(bid);
//				continue;
//			}
//			
//			listBid.add(bid);
//		}
		
		List<Map<String, Object>> sonlist = new TcwWanliOtherPayment()
				.setEffectStock(FlowEnum.EffectStock.EFFECTED.value)
				.setStateRunning(TcwWanliOtherPaymentEnum.StateRunning.NOT.value)
				.where("A.Bid in (" + bids + ")")
				.queryMap();
		model.addAttribute("sonlist", sonlist);
		
		return "finance/tcwWanliOtherPayment/toBatchEditExchangeRate";
	}
}